/*

Copyright (C) 2010  Christoph Schnack 

    This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package VocabularyManagement;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import GuiTools.*;
import Mainframe.Mainframe;
import Settings.Language;
import Settings.Settings;
import Storage.Storage;


class AddVocDialog extends JDialog{
    JDialog avdFrame;
    TribuitTextField nameA,nameB;
    String header_title,f1,f2,vocToEdit,okTxt;
    ActionListener okListener;
    Storage MAINVOC;
    Settings SET;
    Language LANG;
    Mainframe MAINFRAME;
    MDManager VMDM;
    TribuitFrame parent;
    public AddVocDialog(TribuitFrame parentArg,String vte,Storage mainvoc, Settings set,Mainframe mf,MDManager vmdm){
    	parent = parentArg;
    	MAINVOC=mainvoc;
    	SET=set;
    	LANG=SET.getLanguage();
    	MAINFRAME=mf;
    	VMDM=vmdm;
    	vocToEdit = vte;
    	header_title = LANG.getEditVocabulary();
    	okListener = new avdOkEditListener();
    	okTxt = "ok";
    	String[] split = vte.split(" - ");
    	f1 = split[0];
    	f2 = split[1];
    	buildAddVocDialog();
    }
    
    public AddVocDialog(TribuitFrame parentArg,Storage mainvoc, Settings set,Mainframe mf,MDManager vmdm){
    	parent = parentArg;
    	MAINVOC=mainvoc;
    	SET=set;
    	LANG=SET.getLanguage();
    	MAINFRAME=mf;
    	VMDM=vmdm;
    	header_title = LANG.getAddVocabulary();
    	okListener = new avdOkListener();
    	okTxt = "ok";
    	f1 = "";
    	f2 = "";
    	buildAddVocDialog();
    }
    public void buildAddVocDialog(){
    	avdFrame = new JDialog(parent,true);
    	avdFrame.setTitle(header_title);
    	JPanel avdHeaderPanel = new Header(header_title);
    	JLabel nameALabel = new JLabel(LANG.getLanguage()+" A:");
    	nameA = new TribuitTextField(SET,f1,20);
    	nameA.addActionListener(okListener);
    	JPanel nameAPanel = new JPanel();
    	nameAPanel.setLayout(new BorderLayout());
    	nameAPanel.add(BorderLayout.NORTH,nameALabel);
    	nameAPanel.add(BorderLayout.SOUTH,nameA);
    	JLabel nameBLabel = new JLabel(LANG.getLanguage()+" B:");
    	nameB = new TribuitTextField(SET,f2,20);
    	nameB.addActionListener(okListener);
    	JPanel nameBPanel = new JPanel();
    	nameBPanel.setLayout(new BorderLayout());
    	nameBPanel.add(BorderLayout.NORTH,nameBLabel);
    	nameBPanel.add(BorderLayout.SOUTH,nameB);
    	JButton avdOk = new JButton(okTxt);
    	JButton avdCancel = new JButton(LANG.getCancel());
    	avdCancel.addActionListener(new avdCancelListener());
    	avdOk.addActionListener(okListener);
    	JPanel avdButtonpanel = new JPanel();
    	avdButtonpanel.add(avdOk);
    	avdButtonpanel.add(avdCancel);
    	avdButtonpanel.setBackground(GuiTools.BGCOLOR);
    	JPanel avdFieldpanel = new JPanel();
    	avdFieldpanel.setLayout(new GridBagLayout());
    	GridBagConstraints avdGbc = new GridBagConstraints();
    	avdGbc.insets = new Insets(20,20,20,20);
    	avdFieldpanel.add(nameAPanel,avdGbc);
    	avdGbc.gridx = 1;
    	avdFieldpanel.add(nameBPanel,avdGbc);
    	avdFrame.getContentPane().add(BorderLayout.NORTH,avdHeaderPanel);
    	avdFrame.getContentPane().add(BorderLayout.SOUTH,avdButtonpanel);
    	avdFrame.getContentPane().add(BorderLayout.CENTER,avdFieldpanel);
    	nameA.requestFocus();
    	avdFrame.pack();
    	avdFrame.setVisible(true);
    }
    class avdCancelListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
	    	avdFrame.dispose();
		}
    }
    
    class avdOkListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
		    String text1 = nameA.getText();
	    	String text2 = nameB.getText();
	    	if(!GuiTools.checkInput(text1) || text1.equals("")){
	    		GuiTools.showEmptyOrInvalidInputWarning(LANG, avdFrame, LANG.getNewName()+" A");
	    		nameA.selectAll();
	    		nameA.grabFocus();
				return;
			}
	    	if(!GuiTools.checkInput(text2) || text2.equals("")){
	    		GuiTools.showEmptyOrInvalidInputWarning(LANG, avdFrame, LANG.getNewName()+" B");
	    		nameB.selectAll();
	    		nameB.grabFocus();
				return;
	    	}
	    	if(text1.equals(text2)){
	    		JOptionPane.showMessageDialog(avdFrame,LANG.getNamesEqual());
	    		return;
	    	}
	    	if(MAINVOC.getVocabulary(text1+" - "+text2)!=null){
	    		JOptionPane.showMessageDialog(avdFrame,LANG.getVocabularyAlreadyExists());
	    		return;
	    	}
			MAINVOC.addVocabulary(MAINVOC.makeVocabulary(text1,text2));
			avdFrame.dispose();
			MAINFRAME.updateMainframePane(MAINVOC,SET);
		}
    }
    
    class avdOkEditListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
		    String text1 = nameA.getText();
	    	String text2 = nameB.getText();
	    	if(!GuiTools.checkInput(text1)||text1.equals("")){
	    		GuiTools.showEmptyOrInvalidInputWarning(LANG, avdFrame, LANG.getNewName()+" A");
				return;
			}
	    	if(!GuiTools.checkInput(text2) || text2.equals("")){
	    		GuiTools.showEmptyOrInvalidInputWarning(LANG, avdFrame, LANG.getNewName()+" B");
				return;
	    	}
	    	if(text1.equals(text2)){
	    		JOptionPane.showMessageDialog(avdFrame,LANG.getNamesEqual());
	    		return;
	    	}
	    	if(MAINVOC.getVocabulary(text1+" - "+text2)!=null){
	    		JOptionPane.showMessageDialog(avdFrame,LANG.getVocabularyAlreadyExists());
	    		return;
	    	}
			MAINVOC.editVocabularyName(vocToEdit, text1,text2);
			avdFrame.dispose();
		}
    }
    
}//AddVocDialog